<?php
namespace app\shop\controller;
use app\ApiRest;


use app\shop\model\IntegralList;
use app\shop\model\IntegralLog;

use app\shop\model\Signin;

use app\shop\model\SigninRecord;
use think\App;
use think\Request;


class IndexUser extends ApiRest
{

    protected $model;

    protected $address_model;

    protected $cap_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new \app\farm\model\User();



    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-03 13:35
     * @功能说明:积分使用明细
     */
    public function integralList(){

        $input = $this->_param;

        $dis[] = ['user_id','=',$this->getUserId()];

        $dis[] = ['status','=',2];

        $i_model = new IntegralLog();

//        if(!empty($input['add'])&&$input['add']==1){
//
//            $dis[]=['integral_add','>',0];
//
//        }else{
//
//            $dis[]=['integral_add','<',0];
//        }

        $data = $i_model->integralList($dis,15);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 11:33
     * @功能说明:积分商城商品列表
     */
    public function integralGoodsList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['c.start_time','<',time()];

        $dis[] = ['c.end_time','>',time()];

        $dis[] = ['c.status','=',1];

        $dis[] = ['a.status','=',1];

        if(!empty($input['goods_name'])){

            $dis[] = ['a.goods_name','like','%'.$input['goods_name'].'%'];

        }

        if(!empty($input['type'])){

            $dis[] = ['c.type','=',$input['type']];

        }

        $i_model = new IntegralList();

        $data = $i_model->integralGoodsList($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 11:44
     * @功能说明:获取个人信息
     */
    public function userInfo(){

        $data = $this->model->dataInfo(['id'=>$this->getUserId()],'nickName,avatarUrl,integral');

        $signin_model = new Signin();

        $data['signin_integral'] = $signin_model->where(['uniacid'=>$this->_uniacid])->sum('integral');

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 14:14
     * @功能说明:
     */
    public function signinIndex(){

        $input = $this->_param;
        //用户积分
        $user = $this->model->dataInfo(['id'=>$this->getUserId()],'integral,sign_notice,create_time');

        $start_time = strtotime(date("Y-m-01", strtotime($input['day'])));

        $end_time   = strtotime(date("Y-m-t", strtotime($input['day'])));

        $i = 0;

        $sign_record_model = new SigninRecord();

        $signin_model = new Signin();

        $sign_record_model->initSign($this->getUserId());

        while ($start_time<=$end_time){

            $list[$i]['day'] = date('d',$start_time)*1;

            $list[$i]['day_str'] = $start_time;

            $list[$i]['week'] = date('w',$start_time);

            $dis = [

                'user_id' => $this->getUserId(),

                'create_date' => date('Y-m-d',$start_time)
            ];

            $find = $sign_record_model->dataInfo($dis);
            //未在签到时间内
            if($start_time+86400<$user['create_time']||$start_time>time()){

                $list[$i]['status'] = 2;
            //已经签到
            }elseif(!empty($find)){

                $list[$i]['status'] = 1;

            }else{

                $list[$i]['status'] = 0;

            }
            $start_time+=86400;

            $i++;
        }

        $dis = [

            'user_id' => $this->getUserId()
        ];

        $arr['list'] = $list;
        //总签到
        $arr['total_sign_num'] = $sign_record_model->where($dis)->count();

        $dis['status'] = 1;
        //连续签到
        $arr['sign_num'] = $sign_record_model->where($dis)->count();
        //积分规则
        $arr['integral_text'] = $signin_model->where(['uniacid'=>$this->_uniacid])->value('text');

        $arr = array_merge($arr,$user);

        return $this->success($arr);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 15:24
     * @功能说明:用户签到
     */
    public function signin(){

        $input = $this->_param;

        $sign_record_model = new SigninRecord();

        $signin_model = new Signin();

        $date = date('Y-m-d',$input['day']);

        $dis = [

            'user_id' => $this->getUserId(),

            'create_date' => $date
        ];

        $find = $sign_record_model->dataInfo($dis);

        if(!empty($find)){

            $this->errorMsg('你已经签到过了');
        }

        if($input['day']+86400<$this->getUserInfo()['create_time']||$input['day']>time()){

            $this->errorMsg('未在签到时间内');

        }
        //补签
        if(date('Y-m-d')!=date('Y-m-d',$input['day'])){

            $status = 2;

        }else{

            $status = 1;

            $this->model->dataUpdate(['id'=>$this->getUserId()],['sign_time'=>time()]);
        }

        $sign_atv = $signin_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'status'  => $status,

            'create_time' => time(),

            'create_date' => date('Y-m-d',$input['day']),

            'integral'    => $sign_atv['integral']
        ];

        $res = $sign_record_model->dataAdd($insert);

        $id  = $sign_record_model->getLastInsID();

        $i_log_model = new IntegralLog();

        $dis = [

            'user_id' => $this->getUserId(),

            'status'  => 1
        ];
        //连续签到
        $sign_atv['times'] = $sign_record_model->where($dis)->count();

        $i_log_model->integralUserAdd($this->getUserId(),$sign_atv['integral'],$this->_uniacid,2,10,$id,0,$sign_atv);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-22 16:18
     * @功能说明:签到记录
     */
    public function signinRecordList(){

        $sign_record_model = new SigninRecord();

        $dis = [

            'user_id' => $this->getUserId()
        ];

        $data = $sign_record_model->dataList($dis);

        return $this->success($data);

    }











}
